package com.huazi.www.drug.drugy.servlet;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.huazi.www.drug.drugy.entity.Drug_record;
import com.huazi.www.drug.drugy.entity.Y_drugSelect;
import com.huazi.www.drug.drugy.service.DrugService;
import com.huazi.www.drug.drugy.service.impl.DrugServiceImpl;
import com.huazi.www.drug.drugy.util.Page;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @author 波——最帅的男人
 * @version 1.0
 * @date 2021/5/10 10:44
 */
public class DrugServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //修改 respones域的application域的编码集
        resp.setContentType("application/json;charset=UTF-8");
        //修改request域的字符编码集
        req.setCharacterEncoding("utf-8");
        //获取操作符 caozuo
        String caozuo = req.getParameter("caozuo");
        //调用药品业务层的方法
        DrugService service = new DrugServiceImpl();
        if (caozuo == null) {
            //获取当前页
            String currentPage = req.getParameter("currentPage");
            if (currentPage == null) {
                currentPage = "1";
            }
            //这是id  传过来的id stayDrug
            String stayDrug = req.getParameter("stayDrug");
            if (stayDrug == null) {
                stayDrug = "";
            }
            //获取第一个条件  quanbu
            String quanbu = req.getParameter("quanbu");
            if (quanbu == null) {
                quanbu = "";
            }
            //获取第二个条件  quanbu2
            String quanbu2 = req.getParameter("quanbu2");
            if (quanbu2 == null) {
                quanbu2 = "";
            }
            //调用分页方法  把条件全传过去
            Page<Y_drugSelect> page = service.pageList(Integer.parseInt(currentPage), stayDrug, quanbu, quanbu2);
            //保存page对象
            req.setAttribute("pageList", page);
            if (page.getList().size() > 0) {
                //保存编号
                req.setAttribute("biaohao", page.getList().get(0).getId());
            }
            //保存当前页
            req.setAttribute("dangqian", Integer.parseInt(currentPage));
            //转发至  drugSelect.jsp页面
            req.getRequestDispatcher("drug/drugSelect.jsp").forward(req, resp);
        } else if (caozuo.equals("chaoyao")) {
            //获取传过来的id1
            String id1 = req.getParameter("id1");
            //获取传过来的  riqi1  日期
            String riqi1 = req.getParameter("riqi1");
            int i = Integer.parseInt(id1.trim());
            //调用业务层查药的方法
            List<Drug_record> list = service.selectAllDrug(i, riqi1);
            PrintWriter printWriter = resp.getWriter();
            //转换成json数组
            JSONArray array = new JSONArray();
            array.addAll(list);
            printWriter.print(array);
            printWriter.flush();
            printWriter.close();
        } else if (caozuo.equals("xiugai")) {
            //更新状态的操作
            String id2 = req.getParameter("id2");
            String shu1 = req.getParameter("shu1");
            String shu2 = req.getParameter("shu2");
            String riqi2 = req.getParameter("riqi2");
            String[] split1 = shu1.split("-");
            String[] split2 = shu2.split("-");
            PrintWriter printWriter = resp.getWriter();
            String str = "";
            int i = service.selectZhuangtai(Integer.parseInt(id2), riqi2);
            if (i == 0) {
                str = "该患者未缴费!";
            } else if (i == 1) {
                String b1 = service.selectDrugCount(split1);
                if (b1.equals("药品充足!")) {
                    boolean b = service.updateState(Integer.parseInt(id2), riqi2, split1, split2);
                    if (b) {
                        str = "拿药成功!";
                    } else {
                        str = "数据走丢了!";
                    }
                } else {
                    str = b1;
                }
            } else {
                str = "该患者已拿药！";
            }
//            System.out.println(str);
            String s = JSON.toJSONString(str);
            printWriter.print(s);
            printWriter.flush();
            printWriter.close();
        }
    }
}
